import pygame, sys
from pygame.locals import *
import scene
import rendersystem
import movesystem

class App:
    def __init__(self, fps):
        pygame.init()
        self.fps = fps
        self.clock = pygame.time.Clock()
        self.scene = scene.Scene()
        self.render = rendersystem.RenderSystem()
        self.mover = movesystem.MoveSystem()

    def Run(self):
        while True:
            self.clock.tick(self.fps)
            ticks = pygame.time.get_ticks()
            self.ProcKey()
            self.render.Update()
            self.mover.Update()
            self.scene.Update(ticks)

    def ProcKey(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.Exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.scene.KeyDown(event.key)

        keys = pygame.key.get_pressed()
        if keys[K_ESCAPE]:
            self.Exit()
        else:
            for key, flag in enumerate(keys):
                if flag:
                    self.scene.KeyPressed(key)

    def Exit(self):
        pygame.quit()
        sys.exit()